# -*- coding: utf-8 -*-
import base64
import logging

from pipeline.conf import settings
from pipeline.core.flow.activity import Service
from pipeline.component_framework.component import Component

logger = logging.getLogger('celery')

get_client_by_user = settings.ESB_GET_CLIENT_BY_USER

__group_name__ = u"自定义插件(CUSTOM)"


class TestCustomService(Service):
    __need_schedule__ = False

    def execute_pre_process(self, data, parent_data):
        test_input = data.inputs.test_input
        if not test_input.startswith("test_"):
            message = "test_input should start with 'test_'"
            data.set_outputs('ex_data', message)
            return False
        return True

    def execute(self, data, parent_data):
        executor = parent_data.inputs.executor
        biz_cc_id = parent_data.inputs.biz_cc_id
        client = get_client_by_user(executor)

        # test_input = data.inputs.test_input
        test_textarea = data.inputs.test_textarea
        # test_radio = data.inputs.test_radio

        api_kwargs = {
            'biz_biz_id': biz_cc_id,
            # 'executor': executor,
            # 'test_input': test_input,
            # 'test_textarea': test_textarea,
            # 'test_radio': test_radio,

            "script_version_id": 4,
            "script_param": (base64.b64encode(test_textarea.encode())).decode(),
            "account_alias": "root",
            "script_language": 1,
            "callback_url": "http://4128f373w5.zicp.vip/callback_job/",
            "target_server": {
                "ip_list": [
                    {
                        "bk_cloud_id": 0,
                        "ip": "192.168.30.106"
                    }
                ]
            }
        }

        api_result = client.jobv3.fast_execute_script(api_kwargs)
        logger.info('test_api result: {result}, api_kwargs: {kwargs}'.format(result=api_result, kwargs=api_kwargs))
        if api_result['result']:
            data.set_outputs('data', api_result['data'])
            return True
        else:
            data.set_outputs('ex_data', api_result['message'])
            return False

    def outputs_format(self):
        return [
            self.OutputItem(name='执行结果', key='data', type='object')
        ]


class TestCustomComponent(Component):
    name = u"自定义插件测试"
    code = 'test_custom'
    bound_service = TestCustomService
    # form = '%scustom_plugins/plugin.js' % settings.STATIC_URL
    embedded_form = True
    version = '1.1.0'
    form = """ 
       (function(){
           $.atoms.test_custom = [
               {
                   tag_code: "test_textarea",
                   type: "textarea",
                   attrs: {
                       name: gettext("打印文本"),
                       placeholder: gettext("多个用换行分隔"),
                       hookable: true,
                       validation: [
                           {
                               type: "required"
                           }
                       ]
                   }
               }
           ]
       })();
       """